﻿using DelonixRegia.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class _lock : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                Account auth = AccountManagementDBManager.AuthenticateUser(lblUsername.Text, tbxPassword.Text);
                if (auth != null)
                {
                    Session["acc"] = auth;
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    tbxPassword.Attributes.Add("style", "background-color:#FFB2B2;");
                    lockscreenDiv.Attributes.Remove("style");
                }
            }
            else
            {
                Account acc = (Account)Session["acc"];
                Session["acc"] = null;

                if (acc != null)
                {
                    lblUsername.Text = CapitalizeFirstLetter(acc.username) ?? "John Doe";
                }
                else
                    Response.Redirect("SignIn.aspx");
            }
        }

        public string CapitalizeFirstLetter(string s)
        {
            if (String.IsNullOrEmpty(s))
                return s;
            if (s.Length == 1)
                return s.ToUpper();
            return s.Remove(1).ToUpper() + s.Substring(1);
        }
    }
}